package com.xnyzc.lhy.order.service.impl.address.collect;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.order.entity.address.collect.OaCSearchHistory;
import com.xnyzc.lhy.order.entity.result.address.collect.ResultAddressCollect;
import com.xnyzc.lhy.order.mapper.address.collect.OaCSearchHistoryMapper;
import com.xnyzc.lhy.order.service.address.collect.IOaCSearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户搜索地址历史 服务实现类
 * </p>
 *
 * @author bin
 * @since 2019-07-19
 */
@Service
public class OaCSearchHistoryServiceImpl extends ServiceImpl<OaCSearchHistoryMapper, OaCSearchHistory> implements IOaCSearchHistoryService {

    @Autowired
    private OaCSearchHistoryMapper oaCSearchHistoryMapper;

    /**
     * 用户历史查询地点
     * @param userId 用户id
     * @param current 当前页
     * @return
     */
    @Override
    public IPage<OaCSearchHistory> selectHistoryAddressPage(Long userId, long current) {
        IPage<OaCSearchHistory> iPage = new Page<>();
        iPage.setCurrent(current);
        IPage<OaCSearchHistory> page = oaCSearchHistoryMapper.selectPage(
                iPage,
                Wrappers.<OaCSearchHistory>lambdaQuery()
                        .eq(OaCSearchHistory::getDeleted, "0")
                        .eq(OaCSearchHistory::getUserId, userId)
        );
        List<OaCSearchHistory> list = page.getRecords();
        List result = new ArrayList();
        for (OaCSearchHistory oaCSearchHistory : list) {
            ResultAddressCollect resultAddressCollect = new ResultAddressCollect();
            resultAddressCollect.setBigAddress(oaCSearchHistory.getBigAddress());
            resultAddressCollect.setSmallAddress(oaCSearchHistory.getSmallAddress());
            resultAddressCollect.setHistoryId(oaCSearchHistory.getHistoryId());
            resultAddressCollect.setLat(oaCSearchHistory.getLat());
            resultAddressCollect.setLng(oaCSearchHistory.getLng());
            result.add(resultAddressCollect);
        }
        page.setRecords(result);
        return page;
    }
}
